<?php

/*
 * 注意：
 * 图片文件名以纯数字命名，文件名编号从0开始
 */

set_time_limit(0);
define('GEN_SAVE_PATH', dirname(__FILE__) . '/tmp/');

$adTextFile = 'ad.txt'; //保存广告词标题和描述的文件
$content    = file_get_contents($adTextFile);
$spliter    = '*-*-*-*-*-*-*-*-*-*-*'; //广告标题与描述之间的行分隔符，分隔符上边每行是标题，分隔符下边每行是描述

$tmp    = explode($spliter, $content);
$titles = array_values(array_filter(explode("\n", str_replace("\r", '', $tmp[0]))));
$descs  = array_values(array_filter(explode("\n", str_replace("\r", '', $tmp[1]))));

echo 'Gen zn_text 100X20...<br />';
genZnText100X20($titles);

echo 'Gen zn_text 120X20...<br />';
genZnText120X20($titles);

echo 'Gen zn_tw 120X90...<br />';
genZnTw120X90($titles);

echo 'Gen zn_tw 120X120...<br />';
genZnTw120X120($titles);

echo 'Gen zn_tw 200x60...<br />';
genZnTw200X60($titles, $descs);

function genZnText100X20($titles) {
    $tpl      = dirname(__FILE__) . '/tpl/100x20-zn_text.html';
    $fileName = basename($tpl);
    list($name, $ext) = explode('.', $fileName);

    $tplContent = file_get_contents($tpl);
    preg_match('/(<a.*?href=["|\']{0,1})([^"\'<>]{0,})(["|\']{0,1}.*?>)(.*?)<\/a>/', $tplContent, $tmpMatch);

    foreach($titles as $k => $v) {
        $tmpContent = str_replace(
                            array($tmpMatch[1] . $tmpMatch[2] . $tmpMatch[3], $tmpMatch[4]),
                            array($tmpMatch[1] . 'javascript:;' . $tmpMatch[3], $v),
                            $tplContent
                      );
        $tmpFileName = GEN_SAVE_PATH . $name . '-' . $k . 'a.' . $ext;

        file_put_contents($tmpFileName, $tmpContent);
    }

    return true;
}

function genZnText120X20($titles) {
    $tpl      = dirname(__FILE__) . '/tpl/120x20-zn_text.html';
    $fileName = basename($tpl);
    list($name, $ext) = explode('.', $fileName);

    $tplContent = file_get_contents($tpl);
    preg_match('/(<a.*?href=["|\']{0,1})([^"\'<>]{0,})(["|\']{0,1}.*?>)(.*?)<\/a>/', $tplContent, $tmpMatch);

    foreach($titles as $k => $v) {
        $tmpContent = str_replace(
                            array($tmpMatch[1] . $tmpMatch[2] . $tmpMatch[3], $tmpMatch[4]),
                            array($tmpMatch[1] . 'javascript:;' . $tmpMatch[3], $v),
                            $tplContent
                      );
        $tmpFileName = GEN_SAVE_PATH . $name . '-' . $k . 'a.' . $ext;

        file_put_contents($tmpFileName, $tmpContent);
    }

    return true;
}

function genZnTw120X120($titles) {
    $imgPath = '108x88/';
    $tpl      = dirname(__FILE__) . '/tpl/120x120-zn_tw.html';
    $fileName = basename($tpl);
    list($name, $ext) = explode('.', $fileName);

    $tplContent = file_get_contents($tpl);
    preg_match('/(<a.*?href=["|\']{0,1})([^"\'<>]{0,})(["|\']{0,1}.*?>)/', $tplContent, $tmpMatch);
    preg_match('/(<img.*?src=["|\'])(.*?)(["\'].*?>)/', $tplContent, $tmpMatchImg);
    preg_match('/(<b>)(.*?)(<\/b>)/', $tplContent, $tmpMatchTitle);

    foreach($titles as $k => $v) {
        $tmpImgFile = $imgPath . $k . '.gif';
        $a = array(
            $tmpMatch[1] . $tmpMatch[2] . $tmpMatch[3],
            $tmpMatchImg[1] . $tmpMatchImg[2] . $tmpMatchImg[3],
            $tmpMatchTitle[1] . $tmpMatchTitle[2] . $tmpMatchTitle[3]
        );
        $b = array(
            $tmpMatch[1] . 'javascript:;' . $tmpMatch[3],
            $tmpMatchImg[1] . $tmpImgFile . $tmpMatchImg[3],
            $tmpMatchTitle[1] . $v . $tmpMatchTitle[3]
        );
        $tmpContent  = str_replace($a, $b, $tplContent);
        $tmpFileName = GEN_SAVE_PATH . $name . '-' . $k . 'a.' . $ext;

        file_put_contents($tmpFileName, $tmpContent);
    }

    return true;
}

function genZnTw120X90($titles) {
    $imgPath = '116x86/';
    $tpl      = dirname(__FILE__) . '/tpl/120x90-zn_tw.html';
    $fileName = basename($tpl);
    list($name, $ext) = explode('.', $fileName);

    $tplContent = file_get_contents($tpl);
    preg_match('/(<a.*?href=["|\']{0,1})([^"\'<>]{0,})(["|\']{0,1}.*?>)/', $tplContent, $tmpMatch);
    preg_match('/(<img.*?src=["|\'])(.*?)(["\'].*?>)/', $tplContent, $tmpMatchImg);
    preg_match('/(<b>)(.*?)(<\/b>)/', $tplContent, $tmpMatchTitle);

    foreach($titles as $k => $v) {
        $tmpImgFile = $imgPath . $k . '.gif';
        $a = array(
            $tmpMatch[1] . $tmpMatch[2] . $tmpMatch[3],
            $tmpMatchImg[1] . $tmpMatchImg[2] . $tmpMatchImg[3],
            $tmpMatchTitle[1] . $tmpMatchTitle[2] . $tmpMatchTitle[3]
        );
        $b = array(
            $tmpMatch[1] . 'javascript:;' . $tmpMatch[3],
            $tmpMatchImg[1] . $tmpImgFile . $tmpMatchImg[3],
            $tmpMatchTitle[1] . $v . $tmpMatchTitle[3]
        );
        $tmpContent  = str_replace($a, $b, $tplContent);
        $tmpFileName = GEN_SAVE_PATH . $name . '-' . $k . 'a.' . $ext;

        file_put_contents($tmpFileName, $tmpContent);
    }

    return true;
}

function genZnTw200X60($titles, $descs) {
    $imgPath  = '60x58/';
    $tpl      = dirname(__FILE__) . '/tpl/200x60-zn_tw.html';
    $fileName = basename($tpl);
    list($name, $ext) = explode('.', $fileName);

    $tplContent = file_get_contents($tpl);
    preg_match('/(<a.*?href=["|\']{0,1})([^"\'<>]{0,})(["|\']{0,1}.*?>)/', $tplContent, $tmpMatch);
    preg_match('/(<img.*?src=["|\'])(.*?)(["\'].*?>)/', $tplContent, $tmpMatchImg);
    preg_match('/(<h3>)(.*?)(<\/h3>)/', $tplContent, $tmpMatchTitle);
    preg_match('/(<p>)(.*?)(<\/p>)/', $tplContent, $tmpMatchDesc);

    foreach($titles as $k => $v) {
        $tmpImgFile = $imgPath . $k . '.gif';
        $a = array(
            $tmpMatch[1]      . $tmpMatch[2]      . $tmpMatch[3],
            $tmpMatchImg[1]   . $tmpMatchImg[2]   . $tmpMatchImg[3],
            $tmpMatchTitle[1] . $tmpMatchTitle[2] . $tmpMatchTitle[3],
            $tmpMatchDesc[1]  . $tmpMatchDesc[2]  . $tmpMatchDesc[3],
        );
        $b = array(
            $tmpMatch[1]      . 'javascript:;'                    . $tmpMatch[3],
            $tmpMatchImg[1]   . $tmpImgFile                       . $tmpMatchImg[3],
            $tmpMatchTitle[1] . $v                                . $tmpMatchTitle[3],
            $tmpMatchDesc[1]  . implode(' ', getRandDesc($descs)) . $tmpMatchDesc[3],
        );
        $tmpContent  = str_replace($a, $b, $tplContent);
        $tmpFileName = GEN_SAVE_PATH . $name . '-' . $k . 'a.' . $ext;

        file_put_contents($tmpFileName, $tmpContent);
    }

    return true;
}

function getRandDesc($descs, $num = 2) {
    $r = array();

    while(count($r) < $num) {
        $index = array_rand($descs);
        $tmp = trim($descs[$index]);

        if(in_array($tmp, $r)) {
            continue;
        }

        $r[] = $tmp;
    }

    return $r;
}
